<template>
  <div class="app-container">
    <div>
      <!-- 搜索 -->
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="报告编号" prop="reportno">
          <el-input
            v-model="queryParams.reportno"
            placeholder="请输入报告编号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="单位名称" prop="companyname">
          <el-input
            v-model="queryParams.companyname"
            placeholder="请输入单位名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="在岗状态" prop="workstatus">
          <el-select v-model="queryParams.workstatus" placeholder="请选择在岗状态" clearable>
            <el-option
              v-for="dict in dict.type.dime_physical_work"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="开始日期" prop="bcstart">
          <el-date-picker clearable
                          v-model="queryParams.bcstart"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择开始日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="截止日期" prop="bcend">
          <el-date-picker clearable
                          v-model="queryParams.bcend"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择截止日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="编制日期" prop="makedate">
          <el-date-picker clearable
                          v-model="queryParams.makedate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择编制日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="编制人" prop="reportmaker">
          <el-input
            v-model="queryParams.reportmaker"
            placeholder="请输入编制人"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="审核日期" prop="approvedate">
          <el-date-picker clearable
                          v-model="queryParams.approvedate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择审核日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="审核人" prop="reportapporver">
          <el-input
            v-model="queryParams.reportapporver"
            placeholder="请输入审核人"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="审核状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择在岗状态" clearable>
            <el-option
              label="未审核"
              value="1"
            />
            <el-option
              label="已审核"
              value="2"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button class="filter-item" size="mini" type="primary" icon="el-icon-plus" @click="handleCreate">新增
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button class="filter-item" type="danger" icon="el-icon-delete" size="mini" :disabled="multiple"
                     @click="handleDelete">删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button class="filter-item" type="warning" icon="el-icon-s-check" size="mini" :disabled="multiple"
                     @click="handleCheck">审核</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
    </div>
    <el-table
      ref="multipleTable"
      v-loading="listLoading"
      border
      stripe
      :data="list"
      size="small"
      @selection-change="handleSelectionChange"
      @row-click="handleRowClick"
    >
       <el-table-column type="selection" width="44" align="center" />
       <el-table-column label="报告编号" align="center" prop="reportno" width="120"/>
       <el-table-column label="单位名称" align="center" prop="companyname" width="200"/>
       <el-table-column label="报告人数" align="center" prop="reportcount" width="100"/>
       <el-table-column label="在岗状态" align="center" prop="workstatus">
         <template slot-scope="scope">
           <dict-tag :options="dict.type.dime_physical_work" :value="scope.row.workstatus"/>
         </template>
       </el-table-column>
       <el-table-column label="开始日期" align="center" prop="bcstart" width="120">
         <template slot-scope="scope">
           <span>{{ parseTime(scope.row.bcstart, '{y}-{m}-{d}') }}</span>
         </template>
       </el-table-column>
       <el-table-column label="截止日期" align="center" prop="bcend" width="120">
         <template slot-scope="scope">
           <span>{{ parseTime(scope.row.bcend, '{y}-{m}-{d}') }}</span>
         </template>
       </el-table-column>
       <el-table-column label="编制日期" align="center" prop="makedate" width="120">
         <template slot-scope="scope">
           <span>{{ parseTime(scope.row.makedate, '{y}-{m}-{d}') }}</span>
         </template>
       </el-table-column>
       <el-table-column label="编制人" align="center" prop="reportmaker" />
       <el-table-column label="审核日期" align="center" prop="approvedate" width="120">
         <template slot-scope="scope">
           <span>{{ parseTime(scope.row.approvedate, '{y}-{m}-{d}') }}</span>
         </template>
       </el-table-column>
       <el-table-column label="审核人" align="center" prop="reportapporver" />
       <el-table-column label="审核状态" align="center" prop="status" >
         <template slot-scope="scope">
           <el-tag :type="scope.row.status==1?'danger' :'success'">{{ scope.row.status==1?'未审核' :'已审核'}}</el-tag>
         </template>
       </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" icon="el-icon-edit" @click="handleUpdate(row)" />
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="handleDelete(row)" />
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog :visible.sync="dialogFormVisible" :close-on-click-modal="false" @close="cancel()"
               :title="formTitle">
      <el-form ref="form" :model="form" label-width="80px" label-position="right" :rules="rules">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="报告编号" prop="reportno">
              <el-input
                v-model="form.reportno"
                placeholder="请输入报告编号"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单位名称" prop="companyname">
              <el-input
                v-model="form.companyname"
                placeholder="请输入单位名称"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="在岗状态" prop="workstatus">
              <el-select v-model="form.workstatus" placeholder="请选择在岗状态" clearable style="width: 100%">
                <el-option
                  v-for="dict in dict.type.dime_physical_work"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开始日期" prop="bcstart">
              <el-date-picker clearable
                              style="width: 100%"
                              v-model="form.bcstart"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择开始日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="截止日期" prop="bcend">
              <el-date-picker clearable
                              style="width: 100%"
                              v-model="form.bcend"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择截止日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="编制日期" prop="makedate">
              <el-date-picker clearable
                              style="width: 100%"
                              v-model="form.makedate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择编制日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="编制人" prop="reportmaker">
              <el-input
                v-model="form.reportmaker"
                placeholder="请输入编制人"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="审核日期" prop="approvedate">
              <el-date-picker clearable
                              style="width: 100%"
                              v-model="form.approvedate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择审核日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="审核人" prop="reportapporver">
              <el-input
                v-model="form.reportapporver"
                placeholder="请输入审核人"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="审核状态" prop="status">
              <el-select v-model="form.status" placeholder="请选择在岗状态" clearable style="width: 100%">
                <el-option
                  label="未审核"
                  value="1"
                />
                <el-option
                  label="已审核"
                  value="2"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer">
        <el-button  type="text" @click="cancel">取消</el-button>
        <el-button  v-loading="formLoading" type="primary" @click="save">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>

import Pagination from "@/components/Pagination";
import permission from "@/directive/permission/index.js";
import { cloneDeep } from "lodash";
import { listmain,delmain,getInfo,editReport,occupationalHealthApprove } from '@/api/physical/physicaloccupational'

const defaultForm = {
  reportno: null,
  companyname: null,
  workstatus: null,
  bcstart: null,
  bcend: null,
  makedate: null,
  reportmaker: null,
  approvedate: null,
  reportapporver: null,
  status: null
}
export default {
  name: 'Book',
  components: {
    Pagination
  },
  directives: {
    permission
  },
  props: [],
  dicts: [ 'dime_physical_work'],
  data() {
    return {
      dialogFormVisible: false,
      // 选中数组
      ids: [],
      codes:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        reportno: null,
        companyname: null,
        workstatus: null,
        bcstart: null,
        bcend: null,
        makedate: null,
        reportmaker: null,
        approvedate: null,
        reportapporver: null,
        status: null
      },

      rules: {
         reportno: [{
           required: true,
           message: '请输入报告编号',
           trigger: 'blur'
         }],
         companyname: [{
          required: true,
          message: '请输入单位名称',
           trigger: 'blur'
         }],
      },
      form: Object.assign({}, defaultForm),
      list: [],
      totalCount: 0,
      listLoading: false,
      formLoading: false,
      multipleSelection: [],
      formTitle: '修改报告',
      isEdit: false,
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getList()
  },
  mounted() {},
  methods: {
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    getList() {
      this.listLoading = true;
      listmain(this.queryParams).then(response => {
        this.totalCount = response.totalCount;
        this.list=response.rows;
        this.total = response.total;
        this.listLoading = false;
      });
    },
    handleFilter() {
      this.getList();
    },

    handleCreate() {
      this.$router.push(
        {
          path: '/summaryreport/report',
        }
      )
    },

    handleCheck(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否审核所选项？').then(()=> {
        return occupationalHealthApprove(ids)
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("审核成功");
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消审核'
        });
      });
    },
    handleUpdate(row) {
      this.dialogFormVisible=true
      getInfo(row.id).then(response => {
        this.form=response.data;
      });
    },
    /** 提交按钮 */
    save() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let submitForm = JSON.parse(JSON.stringify(this.form))
          editReport(submitForm).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.dialogFormVisible = false;
            this.getList();
          });
        }
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      this.codes=val.map(item => item.reportno)
      this.ids = val.map(item => item.id)
      this.single = val.length!==1
      this.multiple = !val.length
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const codes = row.reportno || this.codes;
      this.$modal.confirm('是否确认删除报告编号为"' + codes + '"的数据项？').then(function() {
        return delmain(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    handleRowClick(row, column, event) {
      this.$refs.multipleTable.clearSelection();
      this.$refs.multipleTable.toggleRowSelection(row);
    },
    cancel() {
      this.form = Object.assign({}, defaultForm);
      this.dialogFormVisible = false;
      this.$refs.form.clearValidate();
    },
  }
}

</script>
<style>
</style>
